# -*- coding:utf-8 -*-
# @Time : 2020/12/9 10:03
# @Author: limeng
# @File : bug_reopen_service.py
from sqlalchemy import desc, or_, func

from application import db
from application.libs import const
from application.models.mantis_models import MantisUserTable
from application.models.models import BugReopen


def bug_reopen_rate():
    """
    开发名下reopen bug数量统计
    :return:
    """
    # m_User_realname = db.session.query(MantisUserTable.realname).filter(
    # or_(MantisUserTable.realname.like('D-%'), MantisUserTable.realname.like('FE%'))).order_by(MantisUserTable
    # .realname).all()
    result = db.session.query(BugReopen, func.COUNT(1).label("bug_count")).group_by(BugReopen.bug_name).order_by(
        desc("bug_count")).all()
    br_list = []
    if result:
        for item in result:
            br_list.append({"date": item.BugReopen.bug_name, "value": item.bug_count})
    data = [{
        "color": "#67C23A",
        "name": const.REOPEN_BUG_COUNT_Y,
        "type": const.TOTAL_CASE_BAR,
        "counter": br_list
    }]
    return data


def bug_reopen_detailed(bug_name):
    """
     reopen明细查询
    :param bug_name:
    :return:
    """

    result = db.session.query(BugReopen).filter(
        BugReopen.bug_name == bug_name).order_by(desc(BugReopen.addtime)).all()
    response_dic = {}
    response_list = []
    if result:
        for msg in result:
            response_dic['bug_id'] = msg.bug_id
            response_dic['bug_title'] = msg.bug_title
            response_dic['bug_name'] = msg.bug_name
            response_list.append(response_dic.copy())
            response_dic.clear()
    response_dic['items'] = response_list
    response_dic['total'] = len(result)
    return response_dic
